<?php
namespace app\admin\controller;

use app\lib\exception\FailedException;
use think\Controller;
use app\common\controller\UploadFile;
use think\Db;
use think\Request;


class Upload extends Controller
{


    /*
     * 上传图片
     * @string user_id 用户id
     * @file   文件
     */
    public function index(Request $request)
    {
        //定义是哪个目录
        $directory = $request->directory;
        if(empty($directory)){
            $directory = "shop";
        }
        $flag = $request->flag;
        $file = $_FILES['file'];
        // 创建时间目录
        $dir = $directory.'/'.date('Ymd');
        $path = './uploads/'.$dir.'/';
        if(!is_dir($path)){
            mkdir($path, 0777, true);
        }
        $upload = new UploadFile(true,$path, array('jpg', 'jpeg', 'png'));
        $path = $upload->upload_file($file);
        if($path){
            $path = ltrim($path,'.');
            return writeJson(200,[
                $flag => $path
            ]);
        }else{
            throw new FailedException([
                'msg' => $upload->get_msg()
            ]);
        }
    }


}